import service from '@/utils/request'
import axios from 'axios';


//布置作业
export const addHomeworkService = (homeworkData) =>{
    return service.post('/homework/addHomework',homeworkData)
}

//查询所有作业
export const gethomeworkService = (courseManageId) => {
    return service.get(`/homework/getAll?courseManageId=${courseManageId}`);
};


//根据作业Id查询作业
export const getHomeworkByIdService = (homeworkId) => {
    return service.get(`/homework/getResult?homeworkId=${homeworkId}`);
};

// 查看学生作业
export const getStuAnswerService = (homeworkId, userId) => {
    // 确保 homeworkId 和 userId 是有效的数字
    if (isNaN(Number(homeworkId)) || isNaN(Number(userId))) {
        console.error("Invalid homeworkId or userId:", homeworkId, userId);
        throw new Error("Invalid homeworkId or userId");
    }

    return service.get(`/homework/getStuAnswer`, {
        params: {
            homeworkId: Number(homeworkId),
            userId: Number(userId)
        }
    });
};

// 学生获取指定课程的所有作业
export const getStudentHomeworkService = (courseManageId) => {
    // 确保 courseManageId 是一个数值而不是对象
    const id = typeof courseManageId === 'object' ? courseManageId.courseManageId : courseManageId;
    return service.get(`/homework/stuGetAll?courseManageId=${id}`);
};


// 批改作业提交
export const updateHomeworkService = (data) => {
    return service.put(`/homework/updateScore`, data);
};

// 学生提交作业
export const submitHomeworkService = (data) => {
    console.log(data)
    return service.post(`/homework/submitHomework`,data);
};